package com.william.jdbcplus.boot.test.dao.impl;

import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootWrap;
import com.william.jdbcplus.boot.test.dao.SysUserRoleDao;
import com.william.jdbcplus.boot.test.entity.SysRole;
import com.william.jdbcplus.boot.test.entity.SysUser;
import com.william.jdbcplus.boot.test.entity.SysUserRole;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@AllArgsConstructor
public class SysUserRoleDaoImpl implements SysUserRoleDao {
    @Override
    public List<String> getRoleByUserName(String username) {
        // @Select("select role_code from sys_role where id in
        // (select role_id from sys_user_role where user_id =
        // (select id from sys_user where username=#{username}))")
        QueryLambdaBootWrap bootstrap = new QueryLambdaBootWrap();
        bootstrap
                .select(SysRole::getRoleCode)
                .from(SysRole.class)
                .in(SysRole::getId, i -> i
                        .select(SysUserRole::getRoleId)
                        .from(SysUserRole.class)
                        .eq(SysUserRole::getUserId, e -> e
                                .select(SysUser::getId)
                                .from(SysUser.class)
                                .eq(SysUser::getUsername, username)
                        )
                );

        List<String> list = bootstrap.queryList(String.class);
        return list;
    }

    @Override
    public List<String> getRoleIdByUserName(String username) {
        // @Select("select id from sys_role where id in
        // (select role_id from sys_user_role where user_id =
        // (select id from sys_user where username=#{username}))")
        QueryLambdaBootWrap bootstrap = new QueryLambdaBootWrap();
        bootstrap
                .select(SysRole::getId)
                .from(SysRole.class)
                .in(SysRole::getId, i -> i
                        .select(SysUserRole::getRoleId)
                        .from(SysUserRole.class)
                        .eq(SysUserRole::getUserId, e -> e
                                .select(SysUser::getId)
                                .from(SysUser.class)
                                .eq(SysUser::getUsername, username)
                        )
                );

        List<String> list = bootstrap.queryList(String.class);
        return list;
    }
}
